package com.yuyou.tas.lesson.service.impl;

import com.yuyou.tas.lesson.mapper.LessonResourceMapper;
import com.yuyou.tas.lesson.service.ILessonResourceService;
import com.yuyou.tas.service.api.lesson.entity.LessonResource;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.tool.Sequence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class  LessonResourceService implements ILessonResourceService{

	@Autowired
	private LessonResourceMapper lessonResourceMapper;

	@Override
	public int insert(LessonResource record) {
		record.setId(Sequence.getSequence().nextId());	
		record.setIsDel(0);
		record.setCreateTime(new Date());
		return lessonResourceMapper.insert(record);
	}

	@Override
	public LessonResource selectByPrimaryKey(Long id) {
		return lessonResourceMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(LessonResource record) {
		return lessonResourceMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ResBoolSimpleInfo<String> addResource(int type,String resourceId,Long organizationId,
			Long lessonClassId, Long userId) {		
		LessonResource lr=new LessonResource();
		lr.setType(type);
		lr.setResourceId(resourceId);
		lr.setAuthOrganizationId(organizationId);
		lr.setAuthUserId(userId);
		lr.setLessonClassId(lessonClassId);
		lr.setIsTemp(0);
		this.insert(lr);
		return ResBoolSimpleInfo.getSuccessInfo();
	}

//	@Override
//	@Transactional(rollbackFor=Exception.class,timeout=60)
//	public ResBoolSimpleInfo<String> addResources(List<String> resourceId,
//			Long organizationId, Long lessonClassId, Long userId) {
//		resourceId.forEach(item ->{			
//			this.addResource(item, organizationId, lessonClassId, userId);
//		});
//		return ResBoolSimpleInfo.getSuccessInfo();
//	}

	@Override
	public LessonResource selectByResourceId(String resourceId) {
		return lessonResourceMapper.selectByResourceId(resourceId);
	}

	@Override
	public ResBoolSimpleInfo<String> delByResourceId(String resourceId) {	
		LessonResource lessonResource=this.selectByResourceId(resourceId);
		lessonResource.setIsDel(1);
		this.updateByPrimaryKeySelective(lessonResource);
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	@Override
	@Transactional(rollbackFor=Exception.class,timeout=60)
	public ResBoolSimpleInfo<String> delByResourceIds(String[] resourceIds) {
		for(String i:resourceIds){
			this.delByResourceId(i);
		}
		return ResBoolSimpleInfo.getSuccessInfo();
	}

	@Override
	public List<LessonResource> selectTemps() {
		return lessonResourceMapper.selectTemps();
	}

	@Override
	public List<String> findOldDataLeave() {
		return lessonResourceMapper.findOldDataLeave();
	}

	@Override
	public ResBoolSimpleInfo<String> addResourceTemp(String resourceId,
			Long organizationId, Long lessonClassId, Long userId) {
		LessonResource lr=new LessonResource();
		lr.setResourceId(resourceId);
		lr.setAuthOrganizationId(organizationId);
		lr.setAuthUserId(userId);
		lr.setLessonClassId(lessonClassId);
		lr.setIsTemp(1);
		this.insert(lr);
		return ResBoolSimpleInfo.getSuccessInfo();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	

}
